package com.liuda.workbench.service.impl;

import com.liuda.commons.utils.DateUtil;
import com.liuda.commons.utils.UUIDUtil;
import com.liuda.settings.domain.User;
import com.liuda.workbench.domain.Customer;
import com.liuda.workbench.domain.Count;
import com.liuda.workbench.domain.TranHistory;
import com.liuda.workbench.domain.Transfer;
import com.liuda.workbench.mapper.CustomerMapper;
import com.liuda.workbench.mapper.TranHistoryMapper;
import com.liuda.workbench.mapper.TransferMapper;
import com.liuda.workbench.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    TransferMapper transferMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    TranHistoryMapper tranHistoryMapper;
    @Override
    public int insertSelective(Map map) {
        User user=(User) map.get("user");
        Customer customer=customerMapper.queryCustomerByName((String)map.get("customerName"));
        if(customer==null){
            customer=new Customer();
            customer.setId(UUIDUtil.createId());
            customer.setCreateBy(user.getId());
            customer.setOwner(user.getId());
            customer.setCreateTime(DateUtil.formatDateTime(new Date()));
            customer.setName((String)map.get("customerName"));
            customerMapper.insertSelective(customer);
        }
        //生成交易
        Transfer transfer=new Transfer();
        transfer.setId(UUIDUtil.createId());
        transfer.setCreateBy(user.getId());
        transfer.setCreateTime(DateUtil.formatDateTime(new Date()));
        transfer.setOwner(user.getId());
        transfer.setMoney((String)map.get("money"));
        transfer.setName((String)map.get("name"));
        transfer.setExpectedDate((String)map.get("expectedDate"));
        transfer.setCustomerId(customer.getId());
        transfer.setStage((String)map.get("stage"));
        transfer.setType((String)map.get("type"));
        transfer.setSource((String)map.get("source"));
        transfer.setActivityId((String)map.get("activityId"));
        transfer.setContactsId((String)map.get("contactsId"));
        transfer.setContactSummary((String)map.get("contactSummary"));
        transfer.setDescription((String)map.get("description"));
        transfer.setNextContactTime((String)map.get("nextContactTime"));
        //生成一条交易历史
        TranHistory tranHistory=new TranHistory();
        tranHistory.setId(UUIDUtil.createId());
        tranHistory.setTranId(transfer.getId());
        tranHistory.setCreateBy(user.getId());
        tranHistory.setCreateTime(DateUtil.formatDateTime(new Date()));
        tranHistory.setStage(transfer.getStage());
        tranHistory.setMoney(transfer.getMoney());
        tranHistory.setExpectedDate(transfer.getExpectedDate());
        tranHistoryMapper.insertSelective(tranHistory);
        return transferMapper.insertSelective(transfer);
    }

    @Override
    public int updateSelective(Map map) {
        User user=(User) map.get("user");
        Customer customer=customerMapper.queryCustomerByName((String)map.get("customerName"));
        if(customer==null){
            customer=new Customer();
            customer.setId(UUIDUtil.createId());
            customer.setCreateBy(user.getId());
            customer.setOwner(user.getId());
            customer.setCreateTime(DateUtil.formatDateTime(new Date()));
            customer.setName((String)map.get("customerName"));
            customerMapper.insertSelective(customer);
        }
        Transfer transfer=new Transfer();
        transfer.setId((String)map.get("id"));
        transfer.setEditBy(user.getId());
        transfer.setEditTime(DateUtil.formatDateTime(new Date()));
        transfer.setMoney((String)map.get("money"));
        transfer.setName((String)map.get("name"));
        transfer.setExpectedDate((String)map.get("expectedDate"));
        transfer.setCustomerId(customer.getId());
        transfer.setStage((String)map.get("stage"));
        transfer.setType((String)map.get("type"));
        transfer.setSource((String)map.get("source"));
        transfer.setActivityId((String)map.get("activityId"));
        transfer.setContactsId((String)map.get("contactsId"));
        transfer.setContactSummary((String)map.get("contactSummary"));
        transfer.setDescription((String)map.get("description"));
        transfer.setNextContactTime((String)map.get("nextContactTime"));
        //交易阶段变化则创建一个交易历史对象
        if(!((String)map.get("oldStage")).equals(transfer.getStage())){
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.createId());
            tranHistory.setTranId(transfer.getId());
            tranHistory.setCreateBy(user.getId());
            tranHistory.setCreateTime(DateUtil.formatDateTime(new Date()));
            tranHistory.setStage(transfer.getStage());
            tranHistory.setMoney(transfer.getMoney());
            tranHistory.setExpectedDate(transfer.getExpectedDate());
            tranHistoryMapper.insertSelective(tranHistory);
        }
        return transferMapper.updateByPrimaryKeySelective(transfer);
    }

    @Override
    public List<Transfer> selectAllForDetail() {
        return transferMapper.selectAllForDetail();
    }

    @Override
    public Transfer selectForDetailById(String id) {
        return transferMapper.selectForDetailById(id);
    }

    @Override
    public int deleteSelected(String[] ids) {
        return transferMapper.deleteSelected(ids);
    }

    @Override
    public List<Count> selectStageCountByGroup() {
        return transferMapper.selectStageCountByGroup();
    }

    @Override
    public List<Transfer> selectByCustomerId(String customerId) {
        return transferMapper.selectByCustomerId(customerId);
    }

    @Override
    public List<Transfer> selectByContactsId(String contactsId) {
        return transferMapper.selectByContactsId(contactsId);
    }
}
